﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Expenses.DAL.Interfaces;
using Expenses.DAL.Repositories;
using Expenses.Domain.Entities;

namespace Expenses.Controllers
{
    public class ExpenseController : Controller
    {
        //
        // GET: /Expense/
        private IExpenseRepository repository;

        public IExpenseRepository Repository
        {
            get { return repository; }
            set { repository = value; }
        }


        public ViewResult Index()
        {
            return View(Repository.GetAll());
        }

        
        public ViewResult Edit(Guid id)
        {
            return View(Repository.GetById(id));
        }

        [HttpPost]
        public ActionResult Edit(Expense expense)
        {
            if (ModelState.IsValid)
            {
                Repository.Update(expense);
                //db.Entry(movie).State = EntityState.Modified;
                //db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(expense);
        }

        [HttpPost]
        public ActionResult Search(string searchString)
        {
            var expenses = Repository.GetAllActive();
            
            return View("Index", expenses.Where( e => e.Name.Contains("Agua")));

        }

        public ActionResult Details(Guid id)
        {

            return View();

        }

        public ActionResult Delete(Guid id)
        {

            return View();

        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Expense expense)
        {
            Repository.Add(expense);

            return View("Index", Repository.GetAll());
        }

    
    }
}
